package com.yugao.fintech.libra.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.libra.system.assembly.SysMicroAppAssembly;
import com.yugao.fintech.libra.system.manager.SysMicroAppManager;
import com.yugao.fintech.libra.system.mapper.MicroAppMapper;
import com.yugao.fintech.libra.system.model.entity.MicroApp;
import com.yugao.fintech.libra.system.model.rqrs.microapp.MicroAppBasePageReq;
import com.yugao.fintech.libra.system.model.rqrs.microapp.MicroAppResp;
import com.yugao.fintech.libra.system.model.rqrs.microapp.MicroAppSaveReq;
import com.yugao.fintech.libra.system.model.rqrs.microapp.MicroAppUpdateReq;
import com.yugao.fintech.libra.system.service.MicroAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微应用
 */
@Slf4j
@Service
public class MicroAppServiceImpl extends ServiceImpl<MicroAppMapper, MicroApp> implements IService<MicroApp>, MicroAppService {
    @Autowired
    private SysMicroAppManager microAppManager;
    @Autowired
    private SysMicroAppAssembly microAppAssembly;

    @Override
    public IPage<MicroAppResp> pageMicroApp(MicroAppBasePageReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<MicroApp> lqw = MicroApp.lqw()
                .ge(Objects.nonNull(beginTime), MicroApp::getCreatedAt, beginTime)
                .le(Objects.nonNull(endTime), MicroApp::getCreatedAt, endTime)
                .orderByDesc(MicroApp::getCreatedAt);
        IPage<MicroApp> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, microAppAssembly::toMicroAppResp);
    }

    @Override
    public List<MicroAppResp> listMicroApp() {
        return this.list().stream()
                .map(microAppAssembly::toMicroAppResp).collect(Collectors.toList());
    }

    @Override
    public MicroAppResp getMicroApp(Long id) {
        MicroApp entity = this.getById(id);
        return microAppAssembly.toMicroAppResp(entity);
    }

    @Override
    public boolean saveMicroApp(MicroAppSaveReq req) {
        MicroApp entity = microAppAssembly.toMicroApp(req);
        return this.save(entity);
    }

    @Override
    public boolean updateMicroApp(MicroAppUpdateReq req) {
        MicroApp entity = microAppAssembly.toMicroApp(req);
        return this.updateById(entity);
    }

    @Override
    public boolean deleteMicroApp(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean batchDeleteMicroApp(List<Long> ids) {
        return this.removeByIds(ids);
    }
}
